package com.gs.wechatdemo.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.wechatdemo.R;
import com.gs.wechatdemo.adapter.base.BaseViewHolder;
import com.gs.wechatdemo.constant.ChatCode;
import com.gs.wechatdemo.db.bean.ChatBean;
import com.gs.wechatdemo.util.DateUtil;

import java.util.List;

public class ChatContentAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private Activity activity;
    private Context context;
    private List<ChatBean> list;

    public ChatContentAdapter(Activity activity, Context context, List<ChatBean> list) {
        this.activity = activity;
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseViewHolder holder = null;
        switch (viewType) {
            case ChatCode.CHAT_MY_TEXT:
                holder = BaseViewHolder.get(context, parent, R.layout.rv_my_text);
                break;
            case ChatCode.CHAT_OTHER_TEXT:
                holder = BaseViewHolder.get(context, parent, R.layout.rv_other_text);
                break;
            case ChatCode.CHAT_MY_IMG:
                holder = BaseViewHolder.get(context, parent, R.layout.rv_my_img);
                break;
            case ChatCode.CHAT_OTHER_IMG:
                holder = BaseViewHolder.get(context, parent, R.layout.rv_other_img);
                break;
            case ChatCode.CHAT_MY_VOICE:
                holder = BaseViewHolder.get(context, parent, R.layout.rv_my_voice);
                break;
            case ChatCode.CHAT_OTHER_VOICE:
                holder = BaseViewHolder.get(context, parent, R.layout.rv_other_voice);
                break;
            case ChatCode.CHAT_TIME:
                holder = BaseViewHolder.get(context, parent, R.layout.rv_time);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, final int position) {
        ChatBean bean = list.get(position);
        if (bean.getViewType() == ChatCode.CHAT_TIME) {
            holder.setText(R.id.tv_time, DateUtil.dateToStringFromChat(bean.getDate()));
        }else {
            holder.setImageResource(activity, R.id.iv_head, bean.getPhoto(), 4, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemChildrenClickListener != null) {
                        onItemChildrenClickListener.OnItemChildrenClickListener(v,position);
                    }
                }
            });
            switch (bean.getViewType()) {
                case ChatCode.CHAT_MY_TEXT:
                case ChatCode.CHAT_OTHER_TEXT:
                    holder.setText(R.id.tv_content, bean.getContent());
                    break;
                case ChatCode.CHAT_MY_IMG:
                    holder.setImageResource(activity, R.id.iv_content, bean.getContent(),
                            bean.getPhotoW(), bean.getPhotoH(), 4, RelativeLayout.LEFT_OF, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (onItemChildrenClickListener != null) {
                                        onItemChildrenClickListener.OnItemChildrenClickListener(v,position);
                                    }
                                }
                            });
                    break;
                case ChatCode.CHAT_OTHER_IMG:
                    holder.setImageResource(activity, R.id.iv_content, bean.getContent(),
                            bean.getPhotoW(), bean.getPhotoH(), 4, RelativeLayout.RIGHT_OF, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (onItemChildrenClickListener != null) {
                                        onItemChildrenClickListener.OnItemChildrenClickListener(v,position);
                                    }
                                }
                            });
                    break;
                case ChatCode.CHAT_MY_VOICE:
                    holder.setVoice(R.id.tv_voice, bean.getContent(), RelativeLayout.LEFT_OF, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ImageView ivPlay = holder.fd(R.id.iv_play);
                            ivPlay.setBackgroundResource(R.drawable.anim_my_play_voice);
                            if (onItemChildrenClickListener != null) {
                                onItemChildrenClickListener.OnItemChildrenClickListener(ivPlay,position);
                            }
                        }
                    });
                    break;
                case ChatCode.CHAT_OTHER_VOICE:
                    holder.setVoice(R.id.tv_voice, bean.getContent(), RelativeLayout.RIGHT_OF, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ImageView ivPlay = holder.fd(R.id.iv_play);
                            ivPlay.setBackgroundResource(R.drawable.anim_other_play_voice);
                            if (onItemChildrenClickListener != null) {
                                onItemChildrenClickListener.OnItemChildrenClickListener(ivPlay,position);
                            }
                        }
                    });
                    break;
            }
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getViewType();
    }

    public void add(ChatBean chatBean) {
        list.add(0,chatBean);
        notifyItemInserted(0);
    }

    protected OnItemClickListener onItemClickListener;//单击事件

    public interface OnItemClickListener {
        void onItemClickListener(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    protected OnItemChildrenClickListener onItemChildrenClickListener;

    public interface OnItemChildrenClickListener {
        void OnItemChildrenClickListener(View v, int position);
    }

    public void setOnItemChildrenClickListener(OnItemChildrenClickListener onItemChildrenClickListener) {
        this.onItemChildrenClickListener = onItemChildrenClickListener;
    }

    protected OnItemLongClickListener onItemLongClickListener;

    public interface OnItemLongClickListener {
        void onItemLongClickListener(View v, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}
